package demo;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{ //泛型 侵入性比较强
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override //重写
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override //重写Comparable里的抽象方法
    public int compareTo(Student o) {
        if(this.age > o.age) {
            return 1;
        }else if(this.age < o.age) {
            return -1;
        }else
            return 0;
        //return 0;
    }
}

//todo:Comparator接口中不止一个抽象方法，为什么只需要实现一个compare方法
class AgeComparator implements Comparator<Student> {//比较器 侵入性比较弱

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name); //String 实现comparable，其中有compareTo
    }
}
public class Test {
    public static void BubbleSort(Student[] comparables) { //冒泡排序
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if(comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Student tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student students[] = new Student[3];
        students[0] = new Student(12,"bitch");
        students[1] = new Student(10,"aitch2");
        students[2] = new Student(11,"citch3");

        BubbleSort(students);
        System.out.println(Arrays.toString(students));
    }

    public static void main5(String[] args) {
        Student students[] = new Student[3];
        students[0] = new Student(12,"bitch");
        students[1] = new Student(10,"aitch2");
        students[2] = new Student(11,"citch3");

        //AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();

        Arrays.sort(students, nameComparator);
        System.out.println(Arrays.toString(students));
    }
    public static void main4(String[] args) {
        Student student1 = new Student(10, "bitch");
        Student student2 = new Student(3, "bitch2");

        if(student1.compareTo(student2) > 0) {
            System.out.println("student1 > student2");
        }else if(student1.compareTo(student2) < 0) {
            System.out.println("student1 < student2");
        }
    }
    public static void main3(String[] args) {
        String[] strings = {"asg", "fasd", "basdf"};//字符串比较大小用compareTo
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }

    public static void main2(String[] args) {//如果以后自定义的类要比较大小，那么一定要让这个类具有比较的功能，此时可以选择实现comparable接口
        Student students[] = new Student[3];
        students[0] = new Student(12,"bitch");
        students[1] = new Student(10,"bitch2");
        students[2] = new Student(11,"bitch3");

        Arrays.sort(students); //源码调用compareTo方法
        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {1, 2, 5, 3, 5, 8, 6};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
